
<div class="dvManageContainer">
	<div id="dvManageUser">
		<?
			$res = mysql_query("SELECT * from users");
			while($r = mysql_fetch_array($res)){
		?>
		
			<div class="dvUserBox shadowMe2">
				<?=$r['u']?>
				<div class="edit" onclick="fn__edit(<?=$r['id']?>, '<?=$r['u']?>', '<?=$r['p']?>')"></div>
				<div class="delete" onclick="do__delete(<?=$r['id']?>)"></div>
			</div>
		<?
			}	
		?>
	</div>
	<div id="dvManagerUserForm" class="shadowMe2">
		<input id="id" type="hidden" />
		<input id="u" type="text" class="inputBox" placeholder="Username" />		
		<input id="p" type="text" class="inputBox" placeholder="Password" />
		<input id="btnAdd" type="button" value="Add" class="buttonBox" onclick="do__create()">
		<input id="btnUpdate" type="button" value="Update" class="buttonBox" onclick="do__update()">
		<input id="btnCancel" type="button" value="Cancel" class="buttonBox" onclick="do__cancel()">
	</div>
</div>
<script language="JavaScript">
	$(document).ready(function(){
		$("#btnCancel, #btnUpdate").hide();
	});
	
	function fn__edit(uid, u, p) {
		$("#id").val(uid);
		$("#u").val(u);
		$("#p").val(p);
		
		$("#btnCancel, #btnUpdate").show();
		$("#btnAdd").hide();
	}
	
	function do__update() {
		u = $("#u").val();
		p = $("#p").val();
		id = $("#id").val();
		
		var url = "<?=getPage("dousers")?>";
		var data = {
			"m" : "e", 
			"u" : u,
			"p" : p,
			"id" : id
		};
				
		$.post(url, data, function(json) {		    
		    if(json.result && json.result == 1){
			    window.setTimeout("location.reload(true)", 300);
			} else {
				alert("Error occur while do the process, contact Admin.")
			}
		}, "json");
	}
	
	function do__cancel() {
		$("#btnCancel, #btnUpdate").hide();
		$("#btnAdd").show();
		$("#id").val("");
		$("#u").val("");		
		$("#p").val("");
	}
	
	function do__create() {
		
		u = $("#u").val();
		p = $("#p").val();
		
		var url = "<?=getPage("dousers")?>";
		var data = {
			"m" : "a", 
			"u" : u,
			"p" : p
		};
		
		$.post(url, data, function(json) {		    
		    if(json.result && json.result == 1){
			    window.setTimeout("location.reload(true)", 300);
			} else {
				alert("Error occur while do the process, contact Admin.")
			}
		}, "json");
	}
	
	function do__delete(id) {
		
		if(!confirm("Are you sure, delete this item ?"))
			return;
				
		var url = "<?=getPage("dousers")?>";
		
		var data = {
			"m" : "d", "id" : id
		};
		
		$.post(url, data, function(json) {		    
		    if(json.result && json.result == 1){
			    window.setTimeout("location.reload(true)", 300);
			} else {
				alert("Error occur while do the process, contact Admin.")
			}
		}, "json");
		
	}
</script>